import 'package:flutter/material.dart';
import '../gen/assets.gen.dart';

class YQStepIndicator extends StatefulWidget {
  final List<String> steps;
  final StepController controller;

  const YQStepIndicator({
    super.key,
    required this.steps,
    required this.controller,
  });

  @override
  State<YQStepIndicator> createState() => _YQStepIndicatorState();
}

class _YQStepIndicatorState extends State<YQStepIndicator> {
  @override
  void initState() {
    super.initState();
    // 确保控制器的总步骤数与步骤列表长度一致
    assert(widget.controller.totalSteps == widget.steps.length, 
      'StepController的totalSteps必须与steps列表长度相同');
  }

  @override
  Widget build(BuildContext context) {
    const double nodeSize = 24.0; // 节点图片的大小
    const double lineHeight = 2.0; // 连线的高度

    return ListenableBuilder(
      listenable: widget.controller,
      builder: (context, child) {
        return Container(
          height: 100,
          padding: const EdgeInsets.only(left: 16, right: 16, top: 24, bottom: 16),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(10),
          ),
          margin: const EdgeInsets.symmetric(horizontal: 16),
          child: Stack(
            children: [
              // 连接线 - 放在Stack的最底部
              Positioned(
                left: 30,
                right: 30,
                // 计算连线的垂直位置，使其与节点圆心对齐
                top: nodeSize / 2 - lineHeight / 2,
                child: Container(
                  height: lineHeight,
                  color: const Color(0xFFF4B739),
                ),
              ),
              // 步骤点和文字 - 放在连线上层
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: List.generate(widget.steps.length, (index) {
                  final isCurrentStep = index == widget.controller.currentStep;
                  return Column(
                    children: [
                      // 步骤点
                      SizedBox(
                        width: nodeSize,
                        height: nodeSize,
                        child: Image.asset(
                          isCurrentStep
                              ? Assets.icons.stepNodeDone.path
                              : Assets.icons.stepNodeUndo.path,
                          width: nodeSize,
                          height: nodeSize,
                        ),
                      ),
                      const SizedBox(height: 4),
                      // 步骤标题
                      Text(
                        widget.steps[index],
                        style: isCurrentStep
                            ? const TextStyle(
                                color: Colors.green,
                                fontWeight: FontWeight.bold,
                                fontSize: 15,
                              )
                            : const TextStyle(
                                color: Colors.black45,
                                fontWeight: FontWeight.bold,
                                fontSize: 15,
                              ),
                      ),
                    ],
                  );
                }),
              ),
            ],
          ),
        );
      },
    );
  }
} 

class StepController extends ChangeNotifier {
  // 当前步骤
  int _currentStep;
  
  // 总步骤数
  final int totalSteps;
  
  StepController({
    required this.totalSteps,
    int initialStep = 0,
  }) : _currentStep = initialStep;

  // 获取当前步骤
  int get currentStep => _currentStep;

  // 是否可以前进到下一步
  bool get canNext => _currentStep < totalSteps - 1;
  
  // 是否可以返回上一步
  bool get canPrevious => _currentStep > 0;

  // 前进到下一步
  void nextStep() {
    if (canNext) {
      _currentStep++;
      notifyListeners();
    }
  }

  // 返回上一步
  void previousStep() {
    if (canPrevious) {
      _currentStep--;
      notifyListeners();
    }
  }

  // 跳转到指定步骤
  void jumpToStep(int step) {
    if (step >= 0 && step < totalSteps) {
      _currentStep = step;
      notifyListeners();
    }
  }

  // 重置到第一步
  void reset() {
    _currentStep = 0;
    notifyListeners();
  }
} 